import * as lib from '../../../components/aotoo/lib/index'
const dateInfoHooks = lib.hooks('date-info', true)
export default function({tapDay, today, funButtons, record, caseKey}){
  const that = this
  const key = caseKey

  funButtons.dot = funButtons.dot.map(item=>{
    if (item.aim) {
      item.aim = item.aim + '?key='+key
    }
    return item
  })

  const modalData = {
    total: 0,
    checked: 'pop_5min',
  }

  const alertsData = dateInfoHooks.getItem('case-alerts')
  const belate = alertsData.belate

  if (belate) {
    modalData.total = belate.length
  }

  if (record) {
    modalData.checked = record.checked
  }

  const checkedTitle = new Map([
    ['pop_5min', '5分钟'],
    ['pop_10min', '10分钟'],
    ['pop_30min', '30分钟'],
    ['pop_60min', '1小时'],
  ])

  // if (record) {
  //   wx.showToast({
  //     title: '重复设置无效',
  //     icon: 'error',
  //     duration: 2000
  //   })
  //   return
  // }
  // if (tapDay.timestamp > today.timestamp) {
  //   wx.showToast({
  //     title: '不能大于今天',
  //     icon: 'error',
  //     duration: 2000
  //   })
  //   return
  // }

  const title = `${tapDay.month}月${tapDay.day}日（${tapDay.lunarDate.ncWeek}）`
  const content = {body: [title], date: tapDay.timestr, itemClass: 'belate', ...funButtons}

  function beforeCallback({createCaseCard}){
    let leakCheckTime = 'pop_5min'
    wx.modalx.showModal({
      content: {
        title: ['总计迟到', {title: `${modalData.total}`, itemClass: 'inner-content-case-title-times'}, ''], 
        body: [
          `当前日期：${tapDay.timestr}${tapDay.timestr === today.timestr ? '(今天)':''}`, 
          {
            itemClass: 'inner-content-case-body',
            title: '迟到:',
            "@list": {
              data: [
                {title: '5分钟', attr: {id: 'pop_5min'}},
                {title: '10分钟', attr: {id: 'pop_10min'}},
                {title: '30分钟', attr: {id: 'pop_30min'}},
                {title: '1小时', attr: {id: 'pop_60min'}},
              ],
              itemMethod: {
                aim(e, param, inst){
                  modalData.checked = e.currentTarget.dataset.id
                  content.body[1] = '迟到'+checkedTitle.get(modalData.checked)
                  inst.siblings().removeClass('active', ()=>{
                    inst.addClass('active');
                  })
                }
              },
              listClass: 'inner-content-case-body-checked',
              itemClass: 'inner-content-case-body-checked-item',
              ready(){
                const checkedIt = this.find({id: modalData.checked})
                checkedIt && checkedIt.addClass('active')
                content.body[1] = '迟到'+checkedTitle.get(modalData.checked)
              }
            },
          },
          // '帮助：在日历中选中日期，添加迟到记录',
          {
            title: '我的迟到记录', 
            itemClass: 'pop-case-scroll-bottom', 
            aim(){
              wx.modalx.closeModal()
              that.restoreCaseButton()
              wx.pageScrollTo({
                selector: '#pits-list-container',
                duration: 300,
                success(){
                  that.listCaseType('belate')
                }
              })
            }
          }, 
        ],
        itemClass: 'inner-content',
      },
      confirmText: '确定',
      cancelText: '取消', 
      height: 64,
      width: 80,
      itemClass: 'popup overjob', 
      mask: false,
      success(res){
        if (res.confirm) {
          if (tapDay.timestamp > today.timestamp) {
            wx.showToast({
              title: '不能大于今天',
              icon: 'error',
              duration: 2000
            })
            return
          }
          createCaseCard({...content, ...modalData})
        }
        that.restoreCaseButton()
      }
    })
  }

  return {key, content, beforeCallback}
}